#include<stdio.h>

//在有序数组中查找具体某个数字n————二分查找的学习

/*1.穷举法-从最开始挨个查找，对数组元素少的还行，元素过多查找就慢，效率低
int main()
{
	int arr[] = {1,2,3,4,5,6,7,8,9,10};
	int n = 0;
	scanf("%d",&n);
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	
	for(i = 0;i <= sz;i++)
	{
		if(arr[i] == n)
		{
			printf("找到了，下标1是：%d",i);
			break;
		}
	}
	
	if(n > sz)
	{
	printf("找不到");
	}
	return 0;
}
*/

//2.二分查找发-
int main()
{
	int arr[] = {1,2,3,4,5,6,7,8,9,10};
	int sz = sizeof(arr) / sizeof(arr[0]);  //数组元素个数
	int left = 0;
	int right = sz - 1;
	
	
	int n = 0;
	scanf("%d",&n);//输入要找的数字
	
	while(left <= right)
	{
		int mid = (left+right) / 2;  //中标
		if(arr[mid] < n)
		{
			left = mid + 1;
		}
		else if(arr[mid] > n)
		{
			right = mid - 1;
		}
		else
		{
			printf("找到了，下标是：%d",mid);
			break;
		}
	}
	
	if(left > right)
	{
		printf("查无此数");
	}
	
	return 0;
}
